
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2016原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.beans.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.io.File;
import java.io.IOException;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

/**
 * Editor for {@code java.io.File}, to directly populate a File property
 * from a Spring resource location.
 *
 * <p>Supports Spring-style URL notation: any fully qualified standard URL
 * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
 *
 * <p><b>NOTE:</b> The behavior of this editor has changed in Spring 2.0.
 * Previously, it created a File instance directly from a filename.
 * As of Spring 2.0, it takes a standard Spring resource location as input;
 * this is consistent with URLEditor and InputStreamEditor now.
 *
 * <p><b>NOTE:</b> In Spring 2.5 the following modification was made.
 * If a file name is specified without a URL prefix or without an absolute path
 * then we try to locate the file using standard ResourceLoader semantics.
 * If the file was not found, then a File instance is created assuming the file
 * name refers to a relative file location.
 *
 * @author Juergen Hoeller
 * @author Thomas Risberg
 * @since 09.12.2003
 * @see java.io.File
 * @see org.springframework.core.io.ResourceEditor
 * @see org.springframework.core.io.ResourceLoader
 * @see URLEditor
 * @see InputStreamEditor
 */
/**
 *｛@code java.io.File｝的编辑器，用于直接填充File属性
 *从Spring资源位置。
 *
 *＜p＞支持Spring风格的URL表示法：任何完全限定的标准URL
 *（“file:”、“http:”等）和Spring的特殊“classpath：”伪URL。
 *
 *<p><b>注意：</b>此编辑器的行为在Spring2.0中发生了变化。
 *以前，它直接从文件名创建文件实例。
 *从Spring 2.0开始，它采用标准的Spring资源位置作为输入；
 *这与URLEditor和InputStreamEditor现在一致。
 *
 *<p><b>注意：</b>在Spring 2.5中进行了以下修改。
 *如果指定的文件名没有URL前缀或没有绝对路径
 *然后我们尝试使用标准ResourceLoader语义来定位该文件。
 *如果找不到该文件，则会创建一个文件实例，假设该文件
 *name指的是相对文件位置。
 *
 *@作者Juergen Hoeller
 *@作者Thomas Risberg
 *@自2003年12月9日
 *@参见java.io.File
 *@参见org.springframework.core.io.ResourceEditor
 *@参见org.springframework.core.io.ResourceLoader
 *@参见URLEditor
 *@参见InputStreamEditor
 */
public class FileEditor extends PropertyEditorSupport {

	private final ResourceEditor resourceEditor;


	/**
	 * Create a new FileEditor, using a default ResourceEditor underneath.
	 */
	/**
	 *使用下面的默认ResourceEditor创建一个新的FileEditor。
	 */
	public FileEditor() {
		this.resourceEditor = new ResourceEditor();
	}

	/**
	 * Create a new FileEditor, using the given ResourceEditor underneath.
	 * @param resourceEditor the ResourceEditor to use
	 */
	/**
	 *使用下面给定的ResourceEditor创建一个新的FileEditor。
	 *@param resourceEditor要使用的resourceEditor
	 */
	public FileEditor(ResourceEditor resourceEditor) {
		Assert.notNull(resourceEditor, "ResourceEditor must not be null");
		this.resourceEditor = resourceEditor;
	}


	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (!StringUtils.hasText(text)) {
			setValue(null);
			return;
		}

		// Check whether we got an absolute file path without "file:" prefix.
		// 检查是否获得了不带“file:”前缀的绝对文件路径。
		// For backwards compatibility, we'll consider those as straight file path.
		// 为了向后兼容，我们将这些视为直接文件路径。
		File file = null;
		if (!ResourceUtils.isUrl(text)) {
			file = new File(text);
			if (file.isAbsolute()) {
				setValue(file);
				return;
			}
		}

		// Proceed with standard resource location parsing.
		// 继续执行标准资源位置分析。
		this.resourceEditor.setAsText(text);
		Resource resource = (Resource) this.resourceEditor.getValue();

		// If it's a URL or a path pointing to an existing resource, use it as-is.
		// 如果是指向现有资源的URL或路径，请按原样使用。
		if (file == null || resource.exists()) {
			try {
				setValue(resource.getFile());
			}
			catch (IOException ex) {
				throw new IllegalArgumentException(
						"Could not retrieve file for " + resource + ": " + ex.getMessage());
			}
		}
		else {
			// Set a relative File reference and hope for the best.
			// 设置一个相对的文件引用并希望得到最好的结果。
			setValue(file);
		}
	}

	@Override
	public String getAsText() {
		File value = (File) getValue();
		return (value != null ? value.getPath() : "");
	}

}
